/*
 * This file is part of connotea-java.
 *
 * connotea-java is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * connotea-java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.connotea;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.restlet.Client;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.resource.Representation;

/**
 * Utility methods for JUnit test cases.
 */
public final class Utils {

    private static final SimpleDateFormat DATE = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss'Z'");

    public static Representation load(String resource) {
        return getRepresentation(getPath(resource));
    }

    public static Date parseDate(String date) {
        try {
            return DATE.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    private static Representation getRepresentation(String resource) {
        return new Client(Protocol.FILE).handle(
                new Request(Method.GET, resource)).getEntity();
    }

    private static String getPath(String resource) {
        try {
            return Utils.class.getResource(resource).toURI().toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private Utils() {
        // private constructor -- class cannot be instantiated
    }
}
